#!/bin/bash

VERSION=3.0.0

# get the non-root user, so we can add the custom filters for them
REGUSER=$(last | grep "logged in" | grep -o '^\S*' | head -1)

# check os type for home directory path
if [[ "$OSTYPE" != "darwin"* ]];
then
  HOME_USER="/home/$REGUSER/autohosts/"
  AUTOHOSTS_PATH="/usr/bin/autohosts"
else
  # macos
  HOME_USER="/Users/$REGUSER/autohosts/"
  AUTOHOSTS_PATH="/usr/local/bin/autohosts"
fi

# initialize reusable stuff, to make the terminal results prettier
function croncheck() {
  crontab -l | grep -q 'autohosts' && EXIST=1 || EXIST=0
}

function green() {
  echo -e "\033[32m$1\033[0m"
}

function red() {
  echo -e "\033[0;31m$1\033[0m"
}

function brown() {
  echo -e "\033[0;33m$1\033[0m"
}

function purple() {
  echo -e "\033[0;35m$1\033[0m"
}

# append a notice to the terminal when an upstream list has been added
function append() {
  green ">> $1 upstream successfully appended..."
}

# check for the existence of the upstream hosts file before downloading, to prevent malformed data
function upstream(){
  # see if we're dealing with a linux-based distro or mac
  if [[ "$OSTYPE" != "darwin"* ]];
  then

    # not a mac, use wget
    if [[ "$(wget --spider "$1"  2>&1 | grep '200 OK')" ]];
    then
      # file exists! collect it
      wget "$1" -O ->> /etc/hosts
      append "$hosts_source"
    else
      logger -s "Autohosts update: $1 appears to be offline, skipping..."
    fi

  else

    # is a mac, use curl, no guarantee gnu tools are installed & unnecessary as a dependency, imo
    if [[ "$(curl -I "$1" 2>/dev/null | head -n 1 | cut -d$' ' -f2)" == '200' ]];
    then
      # file exists! collect it
      curl "$1" >> /etc/hosts
      append "$hosts_source"
    else
      logger -s "Autohosts update: $1 appears to be offline, skipping..."
    fi

  fi
}


if [[ $EUID -ne 0 ]]; then
  red "This script must be ran as root, or with sudo privileges."
  exit 1
fi

### setup ###
# if this is the first run, move to appropriate directory and set a config file
if [ ! -e /etc/autohosts.conf ];
then

  # move the script to /usr/bin so it can be executed as an alias
  if [ -f autohosts ] && [ ! -f "$AUTOHOSTS_PATH" ];
  then
    green "Creating an alias for autohosts.."
    cp autohosts "$AUTOHOSTS_PATH" && chmod +x "$AUTOHOSTS_PATH"
  fi

  # migrate custom filters file to the user's home directory
  if [ -f custom_filters ] && [ ! -d "$HOME_USER" ];
  then
    green "Creating custom filters for $REGUSER..."
    mkdir -p "$HOME_USER" && cp custom_filters "$HOME_USER"custom_filters && cp whitelist "$HOME_USER"whitelist

    green "Creating skeleton upstream file..."
    cp hosts_source "$HOME_USER" && chown "$REGUSER:$REGUSER" -R "$HOME_USER"
  fi

  # double-check to ensure we don't have a conflicting cron
  croncheck

  if [ "$EXIST" == 0 ];
    then

      # see if we are root or sudo; if sudo, we need to append sudo to the cron trigger
      if [ "$(whoami)" == "root" ] ; then
        CRON_PATH="$AUTOHOSTS_PATH"
      else
        CRON_PATH="sudo $AUTOHOSTS_PATH"
      fi

      # if no cronjob exists, create one
      crontab -l | { cat; echo "22 19 * * 0 $CRON_PATH > /dev/null 2>&1"; } | crontab -

      croncheck
      if [ "$EXIST" == 1 ];
      then
        green "Cronjob created..."
      else
        red "There was a problem creating the cronjob, you might have to create it manually!"
      fi

  fi

  purple "Setup is complete!"

fi
### end setup ###

purple "== UPDATING =="

# check hosts_source for a list of preferred upstreams
if [ -f "$HOME_USER"hosts_source ];
then

  # start with a clean slate
  echo "" > /etc/hosts
  green "Obtaining hosts lists from preferred upstream sources..."

  # loop any hosts specified by the user in hosts_source and do an existence check
  for hosts_source in $(cat "$HOME_USER"hosts_source);
  do

    upstream "$hosts_source"

  done
  wait
fi


# courtesy check for localhost to prevent loopback breakage if a preferred upstream doesn't specify such
if  ! grep -q "127.0.0.1 localhost" /etc/hosts;
then
  red "No upstream source has a courtesy entry for localhost!  Adding one..."
  echo "127.0.0.1 localhost" >> /etc/hosts
  echo "255.255.255.255 broadcasthost" >> /etc/hosts
fi


# check for and include any custom filters
if [ -e "$HOME_USER"custom_filters ];
then

  green "Processing custom filters..."
  cat "$HOME_USER"'custom_filters' >> /etc/hosts

else

  brown "No custom filters found at $HOME_USER""custom_filters, skipping..."

fi


# devdom: https://notabug.org/angela/devdom
green "Checking if devdom is installed (will execute devdom hosts update, if so)"

if [ -e /usr/bin/devdom ];
then
  # devdom will echo a notice when ran
  devdom hosts
else
  brown "Devdom is not installed; skipping Devdom hosts."
fi

# apply any whitelisted entries
if [ -s "$HOME_USER"whitelist ];
then

  for whitelist in $(cat "$HOME_USER"whitelist);
  do

    green "Checking for $whitelist.. whitelisting any match."
    if [[ "$OSTYPE" != "darwin"* ]];
    then
      sed -i "/$whitelist/d" /etc/hosts
    else
      # osx is weird and expects an extension to be called, so lets set to null
      sed -i '' "/$whitelist/d" /etc/hosts
    fi

  done
  wait
fi

# set a var that can be later referenced to see if the application has been configured already
echo VERSION=$VERSION > /etc/autohosts.conf

# if this is a mac, refresh the dns cache
if [[ "$OSTYPE" == "darwin"* ]];
then
  killall -HUP mDNSResponder
fi

purple "Hosts update complete!"
